/**
 * 统一类型定义文件
 * @description 整合所有类型定义，提供统一的类型导入入口
 * @author AI Assistant
 * @date 2025-08-18
 */

// ================================
// 基础类型定义
// ================================

/**
 * 用户类型
 */
export type UserType = 'client' | 'agent'

/**
 * 页面跳转模式
 */
export type PageMode = 'navigateTo' | 'redirectTo' | 'reLaunch' | 'switchTab' | 'navigateBack'

/**
 * 请求方法类型
 */
export type RequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'

/**
 * 响应状态类型
 */
export type ResponseStatus = 'success' | 'error' | 'loading' | 'idle'

/**
 * 主题模式
 */
export type ThemeMode = 'light' | 'dark' | 'auto'

/**
 * 语言类型
 */
export type LanguageType = 'zh-CN' | 'en-US' | 'ja-JP'

// ================================
// API相关类型
// ================================

/**
 * API响应基础结构
 */
export interface ApiResponse<T = any> {
    code: number
    message: string
    data: T
    timestamp?: number
    requestId?: string
}

/**
 * 分页参数
 */
export interface PageParams {
    page: number
    limit: number
    total?: number
}

/**
 * 列表响应结构
 */
export interface ListResponse<T = any> {
    data: T[]
    total: number
    page: number
    limit: number
    hasMore?: boolean
}

/**
 * 请求选项
 */
export interface RequestOptions {
    url: string
    method?: RequestMethod
    data?: Record<string, any>
    headers?: Record<string, string>
    timeout?: number
    retries?: number
    cache?: boolean
    cacheTime?: number
    loading?: boolean
    showError?: boolean
}

/**
 * 上传文件选项
 */
export interface UploadOptions {
    url: string
    filePath: string
    name: string
    formData?: Record<string, any>
    header?: Record<string, string>
    timeout?: number
    onProgress?: (progress: number) => void
}

// ================================
// 用户相关类型
// ================================

/**
 * 用户信息
 */
export interface UserInfo {
    id: number | string
    username: string
    nickname: string
    avatar: string
    email?: string
    phone?: string
    gender?: 'male' | 'female' | 'unknown'
    birthday?: string
    address?: string
    signature?: string
    status: 'active' | 'inactive' | 'banned'
    type: UserType
    createdAt: string
    updatedAt: string
}

/**
 * 登录表单
 */
export interface LoginForm {
    username: string
    password: string
    captcha?: string
    remember?: boolean
}

/**
 * 注册表单
 */
export interface RegisterForm {
    username: string
    password: string
    confirmPassword: string
    phone: string
    code: string
    agreement: boolean
}

/**
 * 重置密码表单
 */
export interface ResetPasswordForm {
    phone: string
    code: string
    password: string
    confirmPassword: string
}

// ================================
// 组件相关类型
// ================================

/**
 * 头部组件属性
 */
export interface HeaderProps {
    title?: string
    mode?: 'light' | 'dark'
    height?: number
    leftShow?: boolean
    backgroundColor?: string
    textColor?: string
    isShowHeaderBox?: boolean
    positionState?: 'fixed' | 'absolute' | 'relative'
    isShowShadow?: boolean
}

/**
 * 图片组件属性
 */
export interface ImageProps {
    src: string
    mode?: string
    width?: string
    height?: string
    isIcon?: boolean
    imgs?: string[]
    lazyLoad?: boolean
    fadeShow?: boolean
    preview?: boolean
    borderRadius?: string
    backgroundColor?: string
}

/**
 * 列表组件属性
 */
export interface ListProps<T = any> {
    data: T[]
    loading?: boolean
    finished?: boolean
    error?: boolean
    emptyText?: string
    loadingText?: string
    finishedText?: string
    errorText?: string
    onRefresh?: () => void
    onLoadMore?: () => void
}

/**
 * 空状态组件属性
 */
export interface EmptyProps {
    imgUrl?: string
    imgShow?: boolean
    tips?: string
    height?: number
}

/**
 * 虚拟列表组件属性
 */
export interface VirtualListProps<T = any> {
    data: T[]
    itemHeight: number
    containerHeight: number
    overscan?: number
    onScroll?: (scrollTop: number) => void
    onReachBottom?: () => void
}

// ================================
// 表单相关类型
// ================================

/**
 * 表单验证规则
 */
export interface ValidationRule {
    required?: boolean
    min?: number
    max?: number
    pattern?: RegExp
    validator?: (value: any) => boolean | string
    message?: string
}

/**
 * 表单字段
 */
export interface FormField {
    name: string
    label: string
    type: 'input' | 'textarea' | 'select' | 'radio' | 'checkbox' | 'date' | 'time'
    value?: any
    placeholder?: string
    options?: Array<{ label: string; value: any }>
    rules?: ValidationRule[]
    disabled?: boolean
    readonly?: boolean
}

/**
 * 表单配置
 */
export interface FormConfig {
    fields: FormField[]
    labelWidth?: number
    labelPosition?: 'left' | 'right' | 'top'
    size?: 'small' | 'medium' | 'large'
    disabled?: boolean
}

// ================================
// 文件相关类型
// ================================

/**
 * 文件信息
 */
export interface FileInfo {
    name: string
    size: number
    type: string
    url?: string
    path?: string
    lastModified?: number
    extension?: string
}

/**
 * 上传进度
 */
export interface UploadProgress {
    loaded: number
    total: number
    percentage: number
}

/**
 * 分片上传选项
 */
export interface ChunkedUploadOptions {
    chunkSize?: number
    concurrency?: number
    retries?: number
    onProgress?: (progress: UploadProgress) => void
    onChunkComplete?: (chunkIndex: number) => void
    onComplete?: (result: any) => void
    onError?: (error: Error) => void
}

// ================================
// 性能相关类型
// ================================

/**
 * 性能指标
 */
export interface PerformanceMetrics {
    fps: number
    memory: number
    loadTime: number
    renderTime: number
    networkRequests: number
    errorCount: number
    timestamp: number
}

/**
 * 懒加载选项
 */
export interface LazyLoadOptions {
    threshold?: number
    rootMargin?: string
    once?: boolean
    delay?: number
}

/**
 * 图片优化选项
 */
export interface ImageOptimizationOptions {
    src: string
    quality?: number
    webp?: boolean
    width?: number
    height?: number
    cacheTime?: number
}

// ================================
// 存储相关类型
// ================================

/**
 * 存储选项
 */
export interface StorageOptions {
    prefix?: string
    ttl?: number
    encrypt?: boolean
    compress?: boolean
}

/**
 * 缓存项
 */
export interface CacheItem<T = any> {
    data: T
    expiry: number
    timestamp: number
}

// ================================
// 事件相关类型
// ================================

/**
 * 事件处理器
 */
export type EventHandler<T = any> = (event: T) => void

/**
 * 异步事件处理器
 */
export type AsyncEventHandler<T = any> = (event: T) => Promise<void>

/**
 * 生命周期钩子
 */
export interface LifecycleHooks {
    onLoad?: () => void
    onShow?: () => void
    onHide?: () => void
    onUnload?: () => void
    onReady?: () => void
    onPullDownRefresh?: () => void
    onReachBottom?: () => void
}

// ================================
// 工具类型
// ================================

/**
 * 可选属性
 */
export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>

/**
 * 必需属性
 */
export type Required<T, K extends keyof T> = T & { [P in K]-?: T[P] }

/**
 * 深度可选
 */
export type DeepPartial<T> = {
    [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P]
}

/**
 * 深度必需
 */
export type DeepRequired<T> = {
    [P in keyof T]-?: T[P] extends object ? DeepRequired<T[P]> : T[P]
}

/**
 * 键值对
 */
export type KeyValuePair<K = string, V = any> = {
    key: K
    value: V
}

/**
 * 函数类型
 */
export type Fn<T = any, R = T> = (...args: T[]) => R

/**
 * 异步函数类型
 */
export type AsyncFn<T = any, R = T> = (...args: T[]) => Promise<R>

/**
 * 构造函数类型
 */
export type Constructor<T = {}> = new (...args: any[]) => T

// ================================
// 环境相关类型
// ================================

/**
 * 平台类型
 */
export type PlatformType = 'h5' | 'mp-weixin' | 'mp-alipay' | 'mp-baidu' | 'mp-toutiao' | 'mp-qq' | 'app-plus'

/**
 * 环境配置
 */
export interface EnvironmentConfig {
    PLATFORM: PlatformType
    SUPPORT_WEBP: boolean
    SUPPORT_INTERSECTION_OBSERVER: boolean
    SUPPORT_PERFORMANCE_API: boolean
}

/**
 * 构建环境
 */
export type BuildMode = 'development' | 'production' | 'test'
